package com.webank.wedatasphere.qualitis.handler;

import com.webank.wedatasphere.qualitis.rule.entity.SqlTaskInfo;
import com.webank.wedatasphere.qualitis.util.LocalTimeUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 计算任务处理服务
 */
@Service
public class TaskCalculateService {

    /**
     * 处理固定值计算引擎比较
     */
    public void handlerFixValueCompare(String compareType, String numStrResp, String checkThreshold, SqlTaskInfo info){
        Map<String, String> checkResult = new HashMap<>();
        switch (compareType) {
            case "小于":
                checkResult.put("compareType", numStrResp + " < " + checkThreshold);
                if (Integer.parseInt(numStrResp) < Integer.parseInt(checkThreshold)) {
                    checkResult.put("msg", "校验通过");
                    info.setCheckStatus("校验通过");
                } else {
                    checkResult.put("msg", "校验未通过");
                    info.setCheckStatus("校验未通过");
                }
                info.setResult(checkResult.toString());
                break;
            case "大于":
                checkResult.put("compareType", numStrResp + " > " + checkThreshold);
                if (Integer.parseInt(numStrResp) > Integer.parseInt(checkThreshold)) {
                    checkResult.put("msg", "校验通过");
                    info.setCheckStatus("校验通过");
                } else {
                    checkResult.put("msg", "校验未通过");
                    info.setCheckStatus("校验未通过");
                }
                info.setResult(checkResult.toString());

                break;
            case "小于等于":
                checkResult.put("compareType", numStrResp + " <= " + checkThreshold);
                if (Integer.parseInt(numStrResp) <= Integer.parseInt(checkThreshold)) {
                    checkResult.put("msg", "校验通过");
                    info.setCheckStatus("校验通过");
                } else {
                    checkResult.put("msg", "校验未通过");
                    info.setCheckStatus("校验未通过");
                }
                info.setResult(checkResult.toString());
                break;
            case "大于等于":
                checkResult.put("compareType", numStrResp + " >= " + checkThreshold);
                if (Integer.parseInt(numStrResp) >= Integer.parseInt(checkThreshold)) {
                    checkResult.put("msg", "校验通过");
                    info.setCheckStatus("校验通过");
                } else {
                    checkResult.put("msg", "校验未通过");
                    info.setCheckStatus("校验未通过");
                }
                info.setResult(checkResult.toString());
                break;
            case "等于":
                checkResult.put("compareType", numStrResp + " = " + checkThreshold);
                if (Integer.parseInt(numStrResp) == Integer.parseInt(checkThreshold)) {
                    checkResult.put("msg", "校验通过");
                    info.setCheckStatus("校验通过");
                } else {
                    checkResult.put("msg", "校验未通过");
                    info.setCheckStatus("校验未通过");
                }
                info.setResult(checkResult.toString());
                break;
        }
    }

    public void handlerDayChangeCompare(String compareType, String numStrResp, String checkThreshold, SqlTaskInfo sqlTaskInfo) {
        handlerFixValueCompare(compareType,numStrResp,checkThreshold, sqlTaskInfo);
    }
}
